<!--
Copyright 2017 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<kd-graph-metrics
  [metrics]="cumulativeMetrics"
  *ngIf="showMetrics"
></kd-graph-metrics>

<kd-card
  role="table"
  [hidden]="isHidden()"
>
  <div
    title
    fxLayout="row"
    i18n
  >
    Cron Jobs
  </div>
  <div description>
    <span
      class="kd-muted-light"
      i18n
      >Items:&nbsp;</span
    >{{ totalItems }}
  </div>
  <div actions>
    <kd-card-list-filter></kd-card-list-filter>
  </div>

  <div
    content
    [hidden]="showZeroState()"
  >
    <div
      kdLoadingSpinner
      [isLoading]="isLoading"
    ></div>

    <mat-table
      [dataSource]="getData()"
      [trackBy]="trackByResource"
      matSort
      matSortActive="created"
      matSortDisableClear
      matSortDirection="asc"
    >
      <ng-container matColumnDef="statusicon">
        <mat-header-cell *matHeaderCellDef></mat-header-cell>
        <mat-cell *matCellDef="let cronJob">
          <mat-icon
            [ngClass]="getStatus(cronJob).iconClass"
            [matTooltip]="getStatus(cronJob).iconTooltip"
          >
            {{ getStatus(cronJob).iconName }}
          </mat-icon>
        </mat-cell>
      </ng-container>

      <ng-container matColumnDef="name">
        <mat-header-cell
          *matHeaderCellDef
          mat-sort-header
          class="col-stretch-xl"
          disableClear="true"
          i18n
          >Name</mat-header-cell
        >
        <mat-cell
          *matCellDef="let cronJob"
          class="col-stretch-xl"
        >
          <a
            [routerLink]="getDetailsHref(cronJob.objectMeta.name, cronJob.objectMeta.namespace)"
            queryParamsHandling="preserve"
          >
            {{ cronJob.objectMeta.name }}
          </a>
        </mat-cell>
      </ng-container>

      <ng-container
        *ngIf="shouldShowColumn('namespace')"
        matColumnDef="namespace"
      >
        <mat-header-cell
          *matHeaderCellDef
          class="col-stretch-l col-min-90"
          i18n
          >Namespace</mat-header-cell
        >
        <mat-cell
          *matCellDef="let cronJob"
          class="col-stretch-l col-min-90"
          >{{ cronJob.objectMeta.namespace }}</mat-cell
        >
      </ng-container>

      <ng-container matColumnDef="images">
        <mat-header-cell
          *matHeaderCellDef
          class="col-stretch-xl"
          i18n
          >Images</mat-header-cell
        >
        <mat-cell
          *matCellDef="let cronJob"
          class="col-stretch-xl"
        >
          <kd-chips [map]="cronJob.containerImages"></kd-chips>
        </mat-cell>
      </ng-container>

      <ng-container matColumnDef="labels">
        <mat-header-cell
          *matHeaderCellDef
          class="col-stretch-s"
          i18n
          >Labels</mat-header-cell
        >
        <mat-cell
          *matCellDef="let cronJob"
          class="col-stretch-s"
        >
          <kd-chips [map]="cronJob.objectMeta.labels"></kd-chips>
        </mat-cell>
      </ng-container>

      <ng-container matColumnDef="schedule">
        <mat-header-cell
          *matHeaderCellDef
          class="col-stretch-m"
          i18n
          >Schedule</mat-header-cell
        >
        <mat-cell
          *matCellDef="let cronJob"
          class="col-stretch-m"
        >
          <span>{{ cronJob.schedule }}</span>
          <span *ngIf="!cronJob.schedule">-</span>
        </mat-cell>
      </ng-container>

      <ng-container matColumnDef="suspend">
        <mat-header-cell
          *matHeaderCellDef
          class="col-stretch-m"
          i18n
          >Suspend</mat-header-cell
        >
        <mat-cell
          *matCellDef="let cronJob"
          class="col-stretch-m"
          >{{ !!cronJob.suspend }}</mat-cell
        >
      </ng-container>

      <ng-container matColumnDef="active">
        <mat-header-cell
          *matHeaderCellDef
          mat-sort-header
          disableClear="true"
          class="col-stretch-m"
          i18n
          >Active</mat-header-cell
        >
        <mat-cell
          *matCellDef="let cronJob"
          class="col-stretch-m"
          >{{ cronJob.active }}</mat-cell
        >
      </ng-container>

      <ng-container matColumnDef="lastschedule">
        <mat-header-cell
          *matHeaderCellDef
          class="col-stretch-l"
          i18n
          >Last Schedule</mat-header-cell
        >
        <mat-cell
          *matCellDef="let cronJob"
          class="col-stretch-l"
        >
          <kd-date
            *ngIf="cronJob.lastSchedule"
            [date]="cronJob.lastSchedule"
            relative
          ></kd-date>
          <ng-container *ngIf="!cronJob.lastSchedule">-</ng-container>
        </mat-cell>
      </ng-container>

      <ng-container
        *ngFor="let col of getActionColumns()"
        [matColumnDef]="col.name"
      >
        <mat-header-cell *matHeaderCellDef></mat-header-cell>
        <mat-cell *matCellDef="let cj">
          <kd-dynamic-cell
            [component]="col.component"
            [resource]="cj"
          ></kd-dynamic-cell>
        </mat-cell>
      </ng-container>

      <ng-container matColumnDef="created">
        <mat-header-cell
          *matHeaderCellDef
          mat-sort-header
          disableClear="true"
          class="col-stretch-l"
          i18n
          >Created</mat-header-cell
        >
        <mat-cell
          *matCellDef="let cronJob"
          class="col-stretch-l"
        >
          <kd-date
            [date]="cronJob.objectMeta.creationTimestamp"
            relative
          ></kd-date>
        </mat-cell>
      </ng-container>

      <mat-header-row *matHeaderRowDef="getColumns()"></mat-header-row>
      <mat-row *matRowDef="let row; columns: getColumns()"></mat-row>
    </mat-table>

    <div [hidden]="totalItems <= itemsPerPage">
      <mat-paginator
        [length]="totalItems"
        [pageSize]="itemsPerPage"
        hidePageSize
        showFirstLastButtons
      ></mat-paginator>
    </div>
  </div>

  <div
    content
    [hidden]="!showZeroState()"
  >
    <kd-list-zero-state></kd-list-zero-state>
  </div>
</kd-card>
